- name: check/create instance
  hosts: copr-be-dev.cloud.fedoraproject.org
  user: root
  gather_facts: False

  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - ${private}/vars.yml

  tasks:
  - include: $tasks/persistent_cloud.yml
  - include: $tasks/growroot_cloud.yml

- name: provision instance
  hosts: copr-be-dev.cloud.fedoraproject.org
  user: root
  gather_facts: True
  
  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - ${private}/vars.yml
   - ${vars}/${ansible_distribution}.yml

  tasks:
  - include: $tasks/cloud_setup_basic.yml
  - include: $tasks/iptables.yml

  # packages needed
  - name: add packages for copr-be
    action: yum state=present name=$item
    with_items:
    - ansible
    - lighttpd
    - euca2ools
    - rsync
    - openssh-clients
    - mock
    - yum-utils
    - createrepo
    - python-bunch
    - python-requests
    - git
    - screen
    

  - name: make /srv dirs
    file: state=directory path=$item
    with_items:
    - /srv/copr-work
    - /srv/copr-repo

  
  - name: make copr account
    action: user name=copr state=present system=yes home=/srv/copr-work/copr 
  

  - name: setup dirs there
    action: file state=directory path=/srv/copr-work/copr/$item owner=copr group=copr mode=0700
    with_items: 
    - cloud
    - .ssh

  - name: add copr-buildsys keys to copr user path
    action: copy src=$item dest=/srv/copr-work/copr/cloud/ owner=copr group=copr mode=0600
    with_fileglob: $private/files/openstack/copr-copr/*

  - name: setup privkey for copr user
    action: copy src=$private/files/copr/buildsys.priv dest=/srv/copr-work/copr/.ssh/id_rsa owner=copr group=copr mode=600

  - name: setup copr user ssh config file
    action: copy src=$files/copr/ssh_config dest=/srv/copr-work/copr/.ssh/config  owner=copr group=copr mode=600

  - name: replace bashrc for copr user
    action: copy src=$files/copr/copr_bashrc dest=/srv/copr-work/copr/.bashrc owner=copr group=copr mode=600

  - name: auth_key so we can login to localhost as the copr user from the copr user
    action: authorized_key user=copr key="{{ item }}"
    with_file:
    - ${files}/copr/provision/files/buildsys.pub

  - name: set dir ownership for copr-work and copr-repo
    action: file state=directory path=$item owner=copr group=copr
    with_items:
    - /srv/copr-work
    - /srv/copr-work/copr

  # setup webserver
  - name: add config for copr-repo path
    action: copy src=$files/copr/lighttpd/lighttpd.conf dest=/etc/lighttpd/lighttpd.conf owner=root group=root mode=0644
    notify: 
    - restart lighttpd

  # mime default to text/plain and enable dirlisting for indexes
  - name: update lighttpd configs
    action: copy src=$files/copr/lighttpd/$item dest=/etc/lighttpd/conf.d/$item owner=root group=root mode=0644
    with_items:
    - dirlisting.conf
    - mime.conf
    notify: 
    - restart lighttpd

  - name: start webserver
    action: service state=running enabled=yes name=lighttpd

  # setup dirs for the ansible execution off of provisioning
  - name: dirs from provision
    action: file state=directory path=/srv/copr-work/provision/$item owner=copr group=copr
    with_items:
    - action_plugins
    - library
    - files
    - files/mock
    tags:
    - provision_config

  - name: put ansible.cfg for all this into /etc/ansible/ on the system
    action: copy src=$files/copr/provision/ansible.cfg dest=/etc/ansible/ansible.cfg
    tags:
    - provision_config


  - name: put some files into the provision subdir
    action: copy src=$files/copr/provision/$item dest=/srv/copr-work/provision/$item
    with_items:
    - inventory
    - builderpb.yml
    - terminatepb.yml
    tags:
    - provision_config

  - name: put files into the files subdir off of provisioning
    action: copy src=$item dest=/srv/copr-work/provision/files/
    with_fileglob: $files/copr/provision/files/*
    tags:
    - provision_config

  # ansible lacks a recurse - so we need this until then
  - name: put files into the files/mock subdir off of provisioning
    action: copy src=$item dest=/srv/copr-work/provision/files/mock
    with_fileglob: $files/copr/provision/files/mock/*
    tags:
    - provision_config







  handlers:
  - include: $handlers/restart_services.yml
